//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/1/29.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议方法
    //登录方法
    func visitorWillLogin()
    //注册方法
    optional func visitorWillRegister()
    
}

class VisitorLoginView: UIView {

    //声明代理属性 默认为强引用
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    @objc func loginDidClick(){
        //代理调用协议方法
        visitorDelegate?.visitorWillLogin()
    }
    @objc func registerDidClick(){
        //方法加问号，代表可选方法
        visitorDelegate?.visitorWillRegister?()
    }
    
    /**
     设置UI信息
     */
    func setUIInfo(imageName: String?, tittle: String) {
        circleView.hidden = false
        tipLabel.text = tittle
        if imageName != nil {
            iconView.image = UIImage(named: imageName!)
            circleView.hidden = true
        }else{
            startAnimation()
        }
        
    }
    
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //不移除动画
        animation.removedOnCompletion = false
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.duration = 20
        
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    /**
     重写构造方法
     */
    init(){
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     设置访客视图
     */
    private func setupUI() {
//        backgroundColor = UIColor.orangeColor()
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //设置frame布局失效
        for childView in subviews {
            childView.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置布局
        //VFL
        //布局尽量相对于一个控件
        //"view1.attr1 = view2.attr2 * multiplier + constant"
        //圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //大头标的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //这是文案的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 0))
        
        //设置文案宽度高度约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //这是登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //这是注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置背景视图约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[registerButton]", options: [], metrics: nil, views: ["backView":backView,"registerButton":registerButton]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        loginButton.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerButton.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
    }
    
    //MARK: 懒加载所有的控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())

    private lazy var loginButton: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    
    private lazy var registerButton: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
}
